package com.xiaoyi;

import com.alibaba.fastjson.JSONObject;
import com.xiaoyi.utils.FileUtilsCompat;
import com.xiaoyi.utils.OKHttpPan;
import com.xiaoyi.utils.ProxyCralwerUnusedVPN;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.xml.bind.util.JAXBSource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class test {


    public static void main(String[] args) {

        try {
            SslUtils.ignoreSsl();

            Connection connection = Jsoup.connect("http://www.89ip.cn/index_1.html");

            connection.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            connection.header("Accept-Encoding", "gzip, deflate");
            connection.header("Accept-Language", "zh-CN,zh;q=0.9");
            connection.header("Cache-Control", "no-cache");
            connection.header("Pragma", "no-cache");
            connection.header("Connection", "keep-alive");
//            connection.header("Cookie", "Hm_lvt_994f6022e29faa53cf80c2dcc32d823a=1548465137; Hm_lpvt_994f6022e29faa53cf80c2dcc32d823a=1548465145");
            connection.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");
//            connection.header("referer", "https://www.baidu.com/link?url=7WOiTF7f7TAcVwNUfseGAx_l-BNyh44nOT-VBNSr0fd9zfWMbPqNeOLkQ58dAsoK&wd=&eqid=88328c5100022205000000045c4bb3e2");
            connection.header("Host", "www.89ip.cn");
//            connection.header(":method", "GET");
//            connection.header(":path", "/");
//            connection.header(":scheme", "https");
            Document doc = connection.get();
            System.out.println(doc.toString());
            Elements trs = doc.select("tbody").select("tr");
            for(int i = 0;i<trs.size();i++){
                Elements tds = trs.get(i).select("td");
                System.out.println(tds.get(0).text()+":"+tds.get(1).text());
                checkProxy(tds.get(0).text(),tds.get(1).text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine se = manager.getEngineByName("js");
        String str = "[+!![]]+(true<<true)+[]+[!+[]+!+[]+!+{}+!+[]+!+[]+!+[]]+[!![]+!![]+!![]+!![]+!![]+!![]]+[]+[[!+[]+!+[]+!+[]+!+[]+!+[]]>>true>>false*!![]]+[!+[]+!+[]+!![]+!+[]+!+[]+!![]+!+[]+!+[]]+[+!!![]]+(true<<true)+[]+[!+[]+!+[]+!+{}+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]";// 传 "\u5236", JS是认不出来的。 传"\'u5236\'"
        String result = null;
        try {
            result = (String) se.eval(str);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
    }


    public static void seofangfa() {

        try {
              SslUtils.ignoreSsl();

            Connection connection = Jsoup.connect("https://ip.seofangfa.com/proxy/1.html");

            connection.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            connection.header("Accept-Encoding", "gzip, deflate");
            connection.header("Accept-Language", "zh-CN,zh;q=0.9");
            connection.header("Cache-Control", "no-cache");
            connection.header("Pragma", "no-cache");
            connection.header("Connection", "keep-alive");
            connection.header("Cookie", "Hm_lvt_994f6022e29faa53cf80c2dcc32d823a=1548465137; Hm_lpvt_994f6022e29faa53cf80c2dcc32d823a=1548465145");
            connection.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");
            connection.header("referer", "https://www.baidu.com/link?url=7WOiTF7f7TAcVwNUfseGAx_l-BNyh44nOT-VBNSr0fd9zfWMbPqNeOLkQ58dAsoK&wd=&eqid=88328c5100022205000000045c4bb3e2");
            connection.header(":authority", "ip.seofangfa.com");
            connection.header(":method", "GET");
            connection.header(":path", "/");
            connection.header(":scheme", "https");
            Document doc = connection.get();
            Elements trs = doc.select("tbody").select("tr");
            for(int i = 0;i<trs.size();i++){
                Elements tds = trs.get(i).select("td");
                checkProxy(tds.get(0).text(),tds.get(1).text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine se = manager.getEngineByName("js");
        String str = "[+!![]]+(true<<true)+[]+[!+[]+!+[]+!+{}+!+[]+!+[]+!+[]]+[!![]+!![]+!![]+!![]+!![]+!![]]+[]+[[!+[]+!+[]+!+[]+!+[]+!+[]]>>true>>false*!![]]+[!+[]+!+[]+!![]+!+[]+!+[]+!![]+!+[]+!+[]]+[+!!![]]+(true<<true)+[]+[!+[]+!+[]+!+{}+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]";// 传 "\u5236", JS是认不出来的。 传"\'u5236\'"
        String result = null;
        try {
            result = (String) se.eval(str);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
    }
    //http://www.89ip.cn/index_1.html


    private static void checkProxy(String ip, String port) {
        try {
            //http://1212.ip138.com/ic.asp 可以换成任何比较快的网页
            Document document = Jsoup.connect("https://www.baidu.com")
                    .timeout(2 * 1000)
                    .proxy(ip, Integer.parseInt(port))
                    .get();
            if(null!=document.body().text() &&  !("".equals(document.body().text()))){
                System.out.println(ip+":"+port);
                System.out.println(document.body().text());

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("protocol","http");
                jsonObject.put("ip",ip);
                jsonObject.put("port",port);
                String prox = jsonObject.toJSONString()+"\n";
                FileUtilsCompat.wirteStr2File(prox,FileUtilsCompat.ROOT_PATH + File.separator + "proxis.log",true);
            }
        } catch (Exception e) {
        }
    }
}
